// pages/userInfo/userInfo.js
const app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		mobile_phone: '',
		userInfo: {},
		fileList: [],
		showConfirm: false,
		showImage: '',
		nickName: ''
	},
	handleAdd(e) {
		const {
			fileList
		} = this.data;
		const {
			files
		} = e.detail;
		let _this = this
		let mobile_phone = this.data.mobile_phone
		wx.uploadFile({
			url: `https://adminii.sengain.top/fishApi/user/editUserInfo?mobile_phone=${mobile_phone}`,
			filePath: files[0].url,
			name: 'file',
			header: {
				"Content-Transfer-Encoding": "binary",
				"Content-Type": "application/octet-stream",
				"Content-Disposition": "form-data",
			},
			// formData: { user: 'test' },
			success: function (res) {
				console.log(res);
				let json = JSON.parse(res.data)
				console.log(json);
				_this.setData({
					// fileList: [...fileList, ...files], // 此时设置了 fileList 之后才会展示选择的图片
					showImage: json.data.avatar
				});
			}
		})



		// 方法2：每次选择图片都上传，展示每次上传图片的进度
		// files.forEach(file => this.uploadFile(file))
	},
	onUpload(file) {
		const {
			fileList
		} = this.data;

		this.setData({
			fileList: [...fileList, {
				...file,
				status: 'loading'
			}],
		});
		const {
			length
		} = fileList;

	},
	handleRemove(e) {
		const {
			index
		} = e.detail;
		const {
			fileList
		} = this.data;

		fileList.splice(index, 1);
		this.setData({
			fileList,
		});
	},
	// 退出登录 
	logout() {
		let data = {
			token: wx.getStorageSync('token'),
			mobile_phone: this.data.userInfo.mobile_phone
		}
		app.request('get', '/user/loginout', data, res => {
			wx.setStorageSync('token', '')
			wx.setStorageSync('userInfo', {})
			wx.switchTab({
				url: `/pages/index/index`
			});

		})
	},
	changeInput(e) {
		let fieldName = e.target.dataset.fieldName;
		this.setData({
			nickName: e.detail.value
		})
	},
	//修改名称
	confirmEditName() {
		let mobile_phone = this.data.mobile_phone
		let username = this.data.nickName

		let data = {
			username: username,
		}
		let _this = this
		app.request('post', `/user/editUserInfo?mobile_phone=${mobile_phone}`, data, res => {
			_this.setData({
				showConfirm: false,
				nickName: username
			})
			this.getUserInfo(mobile_phone)
		})
	},

	showDialog() {
		let name = this.data.userInfo.username
		this.setData({
			showConfirm: true,
			nickName: name
		})
	},

	getUserInfo(mobile_phone) {
		let data = {
			token: wx.getStorageSync('token'),
			mobile_phone: mobile_phone
		}
		let _this = this
		app.request('get', '/user/getUserInfo', data, res => {
			let userInfo = res.data.data

			_this.setData({
				'mobile_phone': userInfo.mobile_phone,
				userInfo: userInfo,
				showImage:userInfo.avatar
			})
		})
	},

  toAddress(){
    let mobile_phone=this.data.userInfo.mobile_phone
    wx.navigateTo({
      url: `/pages/address/address?mobile_phone=${mobile_phone}`,
    })
  },

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		this.getUserInfo(options.mobile_phone)
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})